package com.zpz.framework.zpzoauth.config.oauth.authorization.exouath.authenticator;

import com.zpz.framework.zpzoauth.common.verificationcode.ZpzOauthVerificationCode;
import com.zpz.framework.zpzoauth.config.oauth.authorization.exouath.ZpzOauthIntegrationAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.oauth2.common.exceptions.OAuth2Exception;
import org.springframework.stereotype.Component;
/**
 * @author zhangpanzhi
 * @time 2019-07-09
 * @description 未经本人允许请勿随便改动，尊重劳动
 * */
@Component
public class ZpzOauthVerificationCodeIntegrationAuthenticator extends ZpzOauthUsernamePasswordAuthenticator {

    private final static String VERIFICATION_CODE_AUTH_TYPE = "vc";

    @Autowired
    private ZpzOauthVerificationCode zpzOauthVerificationCode ;

    @Override
    public void prepare(ZpzOauthIntegrationAuthentication integrationAuthentication) {
        String vcToken = integrationAuthentication.getAuthParameter("vc_token");
        String vcCode = integrationAuthentication.getAuthParameter("vc_code");
        //验证验证码
        boolean flag = zpzOauthVerificationCode.validate(vcToken, vcCode, null,true);
        if (!flag) {
            throw new OAuth2Exception("验证码错误");
        }
    }

    @Override
    public boolean support(ZpzOauthIntegrationAuthentication integrationAuthentication) {
        return VERIFICATION_CODE_AUTH_TYPE.equals(integrationAuthentication.getAuthType());
    }
}
